﻿using System;
using System.Web;
using IapApp;
using IapApp.Manager;
using IapApp.Util.Logger;
using IapService.Data;
using IapService.Entity;
using log4net;

namespace IapService.Service
{
    public class LoginService
    {
        public static ServiceResponse<User> Login()
        {
            var userName = HttpContext.Current.Request["userName"];
            var userPass = HttpContext.Current.Request["userPass"];
            var response = new ServiceResponse<User>();
            try
            {
                response.Result = new UserData().Get(userName, userPass);
                if(response.Result != null && response.Result.Id > 0)
                {
                    SessionManager.NewSession(response.Result);
                }else
                {
                    response.Status.Code = StatusCode.SESSION_END;
                    response.Status.Message = AppTextResource.LOGIN_ERROR;
                }
            }
            catch (Exception exception)
            {
                Log.Error(exception.Message);
                response.Status.Message = AppTextResource.SERVER_ERROR;
                response.Status.Code = StatusCode.ERROR;
            }
            return response;
        }
    }
}